# -*- coding: utf-8 -*-
# @Date   : 2019/11/8
# @File   : IC26_WhiteUser.py
# @Author : zhaochen
'''eof
name:是否为个人征信白户
code:IC26_WhiteUser_2D
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys


reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def PIMFormat():
    # 身份信息
    try:
        data = IC_DSJ_2D.get("data").get("PIM")[0].get("PB01")[0].get("PB01A")
        if data is None:
            return [{}]
        else:
            return data
    except:
        return [{}]

def PMMFormat():
    # 婚姻状况
    try:
        data = IC_DSJ_2D.get("data").get("PMM")[0].get("PB02")
        if data is None:
            return [{}]
        else:
            return data
    except:
        return [{}]


def identity():
    try:
        PB01A = PIMFormat()
        birthday = PB01A[0].get("PB01AR01")
        eduLevel = PB01A[0].get("PB01AD02") # 学历
        maritalState = PMMFormat() # 婚姻状况
        #
        registeredAddress = PB01A[0].get('PB01AQ03')
        if birthday in null_type_list and eduLevel in null_type_list and maritalState == [{}] and registeredAddress in null_type_list:
            return 1
        else:
            return 0
    except:
        return u'缺失值'



def creditQue():
    data = IC_DSJ_2D['data']
    try:
        PCO = getTable(data, 'PCO', 1)
        if PCO is None:
            return u'缺失值'
        elif PCO == [{}]:
            return 1
        else:
            PC02 = getTable(PCO[0], 'PC02', 0)
            if PC02 is None:
                return u'缺失值'
            elif PC02 == [{}]:
                return 1
            else:
                PC02A = getTable(PC02[0], 'PC02A', 0)
                if PC02A is None:
                    return u'缺失值'
                elif PC02A == [{}]:
                    return 1
                else:
                    PC02AH = getTable(PC02A[0], 'PC02AH', 1)
                    if PC02AH is None:
                        return u'缺失值'
                    elif PC02AH == [{}]:
                        return 1
                    else:
                        factor = []
                        for i in PC02AH:
                            firstMonth = i.get('PC02AR01')
                            if firstMonth in null_type_list:
                                factor.append(None)
                            else:
                                factor.append(firstMonth)
                        if len(set(factor)) == 1 and None in factor:
                            return 1
                        else:
                            return 0
    except:
        return u'缺失值'




def IC26_WhiteUser():
    '''1.身份信息表中，生日、婚姻状况、学历、注册地址全为空
        2.信用提示表中，首笔贷款发放月份、首张贷记卡发卡月份、首张准贷记卡发卡月份全为空
        若上述2条任一满足则为个人征信白户'''

    # factor1 = identity()
    factor2 = creditQue()
    if factor2 == u'缺失值':
        factor = 1
    else:
        if factor2:
            factor = 1
        else:
            factor = 0
    if factor:
        return u'缺失值'
    else:
        return 0

result = IC26_WhiteUser()